package com.quasar.hpatient.module.home_daily;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.CalendarDialog;
import com.quasar.hpatient.dialog.RadioDialog;
import com.quasar.hpatient.dialog.WarningDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 首页 ==> 全部
 * created by kalu on 2018/4/2 11:12
 */
public class HomeDailyActivity extends BaseActivity<HomeDailyPresenter> implements HomeDailyView {

    public final static String INPUT = "INPUT";
    public final static String POSITION = "POSITION";
    public final static int REQUEST_CODE = 1001;
    public final static int RESULT_CODE = 1002;

    @Override
    public int initView() {
        return R.layout.activity_home_daily;
    }

    @Override
    public void initDataLocal() {

        final TextView date = findViewById(R.id.activity_home_all_date);
        getPresenter().initData();
        getPresenter().initTime(this, date);
        date.setOnClickListener(v -> {
            final String trim = date.getText().toString().trim();
            try {
                final int day = Integer.parseInt(trim.substring(trim.length() - 2, trim.length()));
                showCalendar(date, day);
            } catch (Exception e) {
            }
        });

        final NavigationView menu = findViewById(R.id.activity_home_all_menu);
        menu.setOnNavigation2ChangeListener((isBack, isMenu1, isMenu2) -> {
            if (isBack) onBackPressed();
            else if (isMenu1) {
                final RecyclerView recycler = findViewById(R.id.activity_record_recycler);
                getPresenter().saveDaily(HomeDailyActivity.this, recycler);
            } else if (isMenu2) {
                if (getPresenter().canAdd()) showAdd();
                else toast("日常记录已存在");
            }
        });

        final RecyclerView recycler = findViewById(R.id.activity_record_recycler);
        getPresenter().initList(this, recycler);

        findViewById(R.id.home_all_next).setOnClickListener(v -> getPresenter().dailyList(this, recycler, false, true));
        findViewById(R.id.home_all_preview).setOnClickListener(v -> getPresenter().dailyList(this, recycler, true, false));
    }

    @Override
    public void initDataNet() {

        final RecyclerView recycler = findViewById(R.id.activity_record_recycler);
        getPresenter().dailyList(this, recycler, false, false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //getPresenter().dailyList(this, recycler, false, false);
        initDataNet();
    }

    @Override
    public void showCalendar(TextView text, int day) {

        final CalendarDialog dialog = new CalendarDialog(this);
        dialog.setScroll(getDate());
        dialog.show();
        dialog.setOnCalendarChangeListener((years, months, days, maxDay, isClick) -> {
            if (isClick) {
                setDate(years + "-" + months + "-" + days);
                final RecyclerView recycler = findViewById(R.id.activity_record_recycler);
                getPresenter().initData();
                getPresenter().initList(this, recycler);
                getPresenter().dailyList(this, recycler, false, false);
            } else {
                getPresenter().schemelList(dialog, years, months, days, maxDay);
            }
        });
    }

    @Override
    public void showAdd() {

        final RadioDialog dialog = new RadioDialog(this);
        dialog.setOnMethodChangeListener(method -> {
            final RecyclerView recycler = findViewById(R.id.activity_record_recycler);
            getPresenter().addModel(HomeDailyActivity.this, recycler,method);
        });
        //final List<String> list = Arrays.asList("引流量");
        final ArrayList<String> list = new ArrayList<>();
        if (!getPresenter().isRemoveBloodsugarFlag())
            list.add("血  糖");
        if (!getPresenter().isRemoveTrafficFlag())
            list.add("引流量");
        //dialog.setList(list, 0);
        dialog.show();
        dialog.setList(list, 0);
        dialog.setLoop(false);
        dialog.setTitle("日常检查项");
    }

    @Override
    public void showDelete(int position, String str) {
        WarningDialog dialog = new WarningDialog(this);
        dialog.setOnDialogChangeListener((left, right) -> {
            if (right) {
                final RecyclerView recycler = findViewById(R.id.activity_record_recycler);
                final TextView date = findViewById(R.id.activity_home_all_date);
                getPresenter().deleteTraffic(HomeDailyActivity.this, recycler, position, date, str);
            }
        });
        dialog.show();
        dialog.setTitles("确定删除" + str);
        dialog.setButton("否", "是");
    }

    @Override
    public void setDate(String date) {
        final TextView text = findViewById(R.id.activity_home_all_date);
        setText(text, date);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode != REQUEST_CODE || resultCode != RESULT_CODE || null == data)
            return;

        final int intExtra = data.getIntExtra(POSITION, -1);
        if (intExtra == -1) return;

        final String stringExtra = data.getStringExtra(INPUT);

        if (TextUtils.isEmpty(stringExtra)) return;

        final RecyclerView recycler = findViewById(R.id.activity_record_recycler);
        getPresenter().refreshModel(this, recycler, stringExtra, intExtra);
    }

    @Override
    public String getDate() {
        final TextView text = findViewById(R.id.activity_home_all_date);
        return text.getText().toString();
    }
}